#!/usr/bin/php
<?php

class build {
	static $buildver = 'cvs20071114b';
}

$validos = array(
	'linux',
	'freebsd',
	'sunos'
);

$validver = array(
	'2.12',
	'2.10',
	'2.8'
);

if(array_key_exists(1,$_SERVER['argv'])) {
	$os = strtolower($_SERVER['argv'][1]);
} else {
	$os = 'linux';
}

if(array_key_exists(2,$_SERVER['argv'])) {
	$ver = $_SERVER['argv'][2];
} else {
	$ver = '2.12';
}

list($gtkmaj,$gtkmin) = explode('.',$ver);

if(!in_array($os,$validos)) {
	print_r($validos);
	exit(1);
}

if(!in_array($ver,$validver)) {
	print_r($validver);
	exit(1);
}

$filelist = array(
	'install',
	'install.conf',
	'data/php_gtk2.so',
	'data/php.ini.in',
	'demo/hello.phpg',
	'platform/install-libtest.c',
	'platform/install-libtest'
);

$directory = dirname(__FILE__);
$dirdirect = dirname($directory);
$newdir = "phpgtk2-".build::$buildver."-gtk{$ver}-{$os}";

//. prepare install config.
$installconf = file_get_contents('install.conf.in');
$installconf = str_replace('[GTK_MAJ]',$gtkmaj,$installconf);
$installconf = str_replace('[GTK_MIN]',$gtkmin,$installconf);
$installconf = str_replace('[BUILD_VER]',build::$buildver,$installconf);
file_put_contents('install.conf',$installconf);
unset($installconf);

//. prepare a fake installation directory via symlink.
system("ln -s {$directory} {$dirdirect}/{$newdir}");

//. rename os dependend files for use.
system("mv data/php_gtk2.{$os}-{$ver} data/php_gtk2.so");
system("mv platform/install-libtest-{$os} platform/install-libtest");

//. targ!
system("tar -C {$dirdirect} -chjf {$newdir}.tar.bz2 {$newdir}/".implode(" {$newdir}/",$filelist));

//. put the os dependent files back where they were.
system("mv data/php_gtk2.so data/php_gtk2.{$os}-{$ver}");
system("mv platform/install-libtest platform/install-libtest-{$os}");
unlink('install.conf');

//. get rid of the fake installation directory link.
system("rm {$dirdirect}/{$newdir}");

?>